package com.haut.servlet;

import com.alibaba.fastjson.JSON;
import com.haut.bean.Leave;
import com.haut.service.LeaveService;
import com.haut.service.impl.LeaveServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet({"/leave/update","/leave/select","/leave/load","/leave/add"})
public class LeaveServlet extends HttpServlet {
    LeaveService leaveService=new LeaveServiceImpl();

    protected void service (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String servletPath = request.getServletPath();
        if("/leave/update".equals(servletPath)){
            doUpdate(request,response);
        }else if("/leave/select".equals(servletPath)){
            doSelect(request,response);
        }else if("/leave/load".equals(servletPath)){
            doLoad(request,response);
        }else if("/leave/add".equals(servletPath)){
            doAdd(request,response);
        }
    }
    private void doAdd(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
        String staff_id=request.getParameter("staff_id");
        String start=request.getParameter("start");
        String end=request.getParameter("end");
        String reason=request.getParameter("reason");
        leaveService.add(new Leave(leaveService.select().size()+1,staff_id,Integer.parseInt(staff_id),null,start,end,reason,0));
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().write("1");
    }
    private void doUpdate(HttpServletRequest request,HttpServletResponse response) throws IOException {
        int id=Integer.parseInt(request.getParameter("id"));
        int name_id= Integer.parseInt(request.getParameter("name_id"));
        String start=request.getParameter("start");
        String end=request.getParameter("end");
        String reason=request.getParameter("reason");
        int state= Integer.parseInt(request.getParameter("mystate"));
        leaveService.updateByAdmin(new Leave(id,String.valueOf(name_id),name_id,null,start,end,reason,state));
        response.setContentType("text/html;charset=UTF-8");
        String json;
        if(state==1){
            json="1";
        }else if(state==-1){
            json="-1";
        }else{
            json="0";
        }
        response.getWriter().write(json);
    }
    private void doSelect(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        List<Leave> leaves=leaveService.select();
        List<Leave> leaveList=new ArrayList<>();
        int staff_id=Integer.parseInt(request.getParameter("id"));
        response.setContentType("text/html;charset=UTF-8");
        String json;
        if(staff_id==1){
            json=JSON.toJSONString(leaves);
        }else {
            for(Leave leave:leaves){
                if(leave.getName_id()==staff_id){
                    leaveList.add(leave);
                }
            }
            json= JSON.toJSONString(leaveList);
        }
        response.getWriter().write(json);
    }
    private void doLoad(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        List<Leave> leaves=leaveService.select();
        request.setAttribute("leaves",leaves);
        request.getRequestDispatcher("/leave.jsp").forward(request,response);
    }
}
